package com.slz3.business.module.wmsManagement.wmsStockTransfer.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsStockTransfer.domain.WmsStockTransfer;
import com.slz3.business.module.wmsManagement.wmsStockTransfer.domain.WmsStockTransferFactory;
import com.slz3.business.module.wmsManagement.wmsStockTransfer.steward.WmsStockTransferSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class WmsStockTransferServiceImpl implements WmsStockTransferService {
    @Override
    public WmsStockTransfer save(WmsStockTransfer wmsStockTransfer) {
        return Repositorys.wmsStockTransfer.save(WmsStockTransferFactory.initBaseFields(wmsStockTransfer));
    }

    @Override
    public Page<WmsStockTransfer> page(MultiValueMap<String, String> params) {
        Page<WmsStockTransfer> wmsStockTransferPage = Repositorys.wmsStockTransfer.findAll(
                WmsStockTransferSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsStockTransferPage;
    }


    @Override
    public WmsStockTransfer modify(WmsStockTransfer wmsStockTransferNew) {
        return Repositorys.wmsStockTransfer.saveAndFlush(WmsStockTransferFactory.initBaseFields(wmsStockTransferNew));
    }

    @Override
    public Optional<WmsStockTransfer> single(UUID uuid) {
        return Repositorys.wmsStockTransfer.findById(uuid);
    }

    @Override
    public List<WmsStockTransfer> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsStockTransfer.findAllById(uuids);
    }

    @Override
    public List<WmsStockTransfer> delete(List<WmsStockTransfer> wmsStockTransferList) {
        wmsStockTransferList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsStockTransferFactory.initBaseFields(item);
        });
        return Services.wmsStockTransferTransactional.batchModify(wmsStockTransferList);
    }

}
